package com.swiftops.basic.sys.controller;

import com.swiftops.basic.sys.service.SysDictService;
import com.swiftops.basic.sys.vo.entity.SysDictEntityVO;
import com.swiftops.basic.sys.vo.request.SysDictRequestVO;
import com.swiftops.basic.sys.vo.response.SysDictResponseVO;
import com.swiftops.common.core.ApiResult;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 字典
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "字典")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @ApiOperation("获取字典分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysDict", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageResult<SysDictResponseVO>>> getSysDictPageList(SysDictRequestVO requestVO) {
        try {
            PageResult<SysDictResponseVO> pageResult = this.sysDictService.querySysDictPageList(requestVO);
            return ApiResult.ok(pageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysDict/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<SysDictEntityVO>> getSysDict(@PathVariable("id") String id) {
        try {
            SysDictEntityVO sysDictEntityVO = this.sysDictService.getSysDictEntityVO(id);
            return ApiResult.ok(sysDictEntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysDict", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> addSysDict(@RequestBody @Validated SysDictEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysDictService.addSysDict(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysDict", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> updateSysDict(@RequestBody @Validated SysDictEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysDictService.updateSysDict(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("启用字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/SysDict/enable/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> enableSysDict(@PathVariable("id") String id) {
        try {
            this.sysDictService.useableSysDict(id, true);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("禁用字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/SysDict/disable/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> disableSysDict(@PathVariable("id") String id) {
        try {
            this.sysDictService.useableSysDict(id, false);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("删除字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysDict/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> deleteSysDict(@PathVariable("id") String id) {
        try {
            this.sysDictService.deleteSysDict(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
